<?php
namespace Admin\Controller;
use Think\Controller;
class ProductController extends Controller {
	public function __construct(){
		parent::__construct();
		if(!$_SESSION['admin_is_login']){
			$this->error("没有登录，请用户去登录。",U("Admin/auth/login"));
		}
	}
    public function index(){
		$products=M('product')->select();
		$this->assign('products',$products);
		$this->display();
    }
    public function add(){
		$products=M('product')->select();
		$this->assign('level1',M('product_catalog')->where("parent_id = 0")->select());
		$this->assign('products',$products);
		$this->display();
    }
    public function do_add(){
		$products=M('product')->select();
		$this->assign('level1',M('product_catalog')->where("parent_id = 0")->select());
		$this->assign('products',$products);
		$this->display();
    }
    public function edit($id){
		$this->assign('thisCatalog',M('product_catalog')->where("id = ".$id)->find());
		$this->assign('level1',M('product_catalog')->where("parent_id = 0")->select());
		$this->display();
    }
    public function do_edit(){
		$id = I('post.id');
		$name = I('post.name');
		$parent_id = I('post.parent_id');
		M('product_catalog')->where('id ='.$id)->save(array('name' => $name, 'parent_id' => $parent_id));
		$this->success("编辑分类成功！",U("Admin/Catalog/index"));
    }
    public function delete($id){
		M('product_catalog')->where('id ='.$id)->delete();
		$this->success("删除分类成功！",U("Admin/Catalog/index"));
    }
    public function getCatalog(){
		$id = I('get.id');
		$data=M('product_catalog')->where("parent_id = ".$id)->select();
		$this->ajaxReturn($data);
    }
}